package com.app.aipass.fragment.my.paytype.addpaytype;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.FragmentAddBankBinding;
import com.app.aipass.entity.Message;
import com.app.aipass.entity.PayAccountEntity;
import com.app.aipass.utils.StatusBarUtil;
import com.app.aipass.utils.TextUtil;
import com.app.aipass.utils.ToastUtil;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;

/**
 * @author: woking
 * @date: 2021/6/11
 * @description：
 */
public class AddBankFragment extends BaseFragment<FragmentAddBankBinding, AddPayTypeViewModel> {
    private Disposable mSubscription;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_add_bank;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public AddPayTypeViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(AddPayTypeViewModel.class);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mSubscription != null)
        {
            RxSubscriptions.remove(mSubscription);
        }
    }
    private long idPayAccount = -1;
    private void setEnable(boolean isEnAble)
    {
        binding.userNameEt.setEnabled(isEnAble);
        binding.bankNoEt.setEnabled(isEnAble);
        binding.bankNameEt.setEnabled(isEnAble);
        binding.bankInfoEt.setEnabled(isEnAble);

        if(isEnAble)
        {
            binding.codeRl.setVisibility(View.VISIBLE);
            binding.saveTv.setVisibility(View.VISIBLE);
        }
        else
        {
            binding.codeRl.setVisibility(View.GONE);
            binding.saveTv.setVisibility(View.GONE);
        }
    }
    @Override
    public void initData() {
        super.initData();
        if (getArguments() != null) {
            PayAccountEntity entity = (PayAccountEntity)getArguments().getSerializable("entity");
            binding.userNameEt.setText(entity.name);
            binding.bankNoEt.setText(entity.account);
            binding.bankNameEt.setText(entity.address);
            binding.bankInfoEt.setText(entity.branch);

            binding.tvTitleText.setText("银行卡");
            idPayAccount = entity.id;
            setEnable(false);
        }
        if(idPayAccount != -1)
        {
            binding.tvRightText.setVisibility(View.VISIBLE);
            binding.tvRightText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(binding.saveTv.getVisibility() == View.VISIBLE)
                    {
                        binding.tvRightText.setText("编辑");
                        setEnable(false);
                    }
                    else
                    {
                        binding.tvRightText.setText("取消");
                        setEnable(true);
                    }
                }
            });
        }
        else
        {
            binding.tvRightText.setVisibility(View.GONE);
        }

        mSubscription = RxBus.getDefault().toObservable(Message.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<Message>() {
                    @Override
                    public void accept(final Message message) throws Exception {
                        Log.e("TAG","===============message=============="+message.getName());
                        if(message.getName().equals("update_pay_type"))
                        {
                            getActivity().finish();
                        }
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);

        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.getcodeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.smsCode((TextView)view);
            }
        });

        binding.saveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                payAccountCreate();
            }
        });
    }

    private boolean parseInfo()
    {
        String account = binding.bankNoEt.getText().toString();
        if(TextUtil.isEmpty(account))
        {
            ToastUtil.showToast("请输入银行卡号");
            return false;
        }
        String name = binding.userNameEt.getText().toString();
        if(TextUtil.isEmpty(name))
        {
            ToastUtil.showToast("请输入开户人姓名");
            return false;
        }
        String address = binding.bankNameEt.getText().toString();
        if(TextUtil.isEmpty(address))
        {
            ToastUtil.showToast("请输入开户银行");
            return false;
        }
        String branch = binding.bankInfoEt.getText().toString();
        if(TextUtil.isEmpty(branch))
        {
            ToastUtil.showToast("请输入开户行分行");
            return false;
        }
        String code = binding.codeEt.getText().toString();
        if(TextUtil.isEmpty(code))
        {
            ToastUtil.showToast("请输入验证码");
            return false;
        }
        return true;
    }
    private void payAccountCreate()
    {
        if(!parseInfo())
        {
            return;
        }
        HashMap<String, Object> map = new HashMap<>();
        //银行卡 {"type": 1,"account": "Entity","name": "Unpro","address": "dsfdsfds","branch": "branch","code": 1234}
        map.put("type", 1);
//        map.put("account", "622161616166161");
//        map.put("name", "吕迅");
//        map.put("address", "eeeeeeee");
//        map.put("branch", "平安支行");
//        map.put("code", "88888888");
        map.put("account", binding.bankNoEt.getText().toString());
        map.put("name", binding.userNameEt.getText().toString());
        map.put("address", binding.bankNameEt.getText().toString());
        map.put("branch", binding.bankInfoEt.getText().toString());
        map.put("code", binding.codeEt.getText().toString());
        map.put("image","aabb.png");
        if(idPayAccount != -1)
        {
            map.put("id", idPayAccount+"");
        }
        viewModel.payAccountCreate(1,map);
    }
}
